package com.zhiyou100.dao.Impl;

import com.zhiyou100.JDBCUtil.DBUtil;
import com.zhiyou100.dao.UserDao;
import com.zhiyou100.emtity.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @description:
 * @author: 马跃
 * @create: 2021-01-20 10:23
 **/
public class UserDaoImpl implements UserDao {

    private QueryRunner qur = new QueryRunner(DBUtil.getDataSource());

    @Override
    public List<User> queryAll() {
        String sql = "select * from user";
        try {
            List<User> query = qur.query(sql, new BeanListHandler<>(User.class));
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void delete(int id) {
        String sql = "delete from user where u_id = ?";
        try {
            qur.update(sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }

    @Override
    public void add(User user) {
        String sql = "insert into user values(?,?,?,?,?,?,?)";
        try {
            qur.update(sql,user.getU_id(),user.getU_account(),user.getU_password(),user.getU_name(),user.getU_age(),user.getU_phone(),user.getU_status());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }

    @Override
    public User query(int id) {
        String sql = "select * from user where u_id = ?";
        try {
            User query = qur.query(sql, new BeanHandler<>(User.class),id);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void upDate(User user) {
        String sql = "update user set u_account=?,u_password=?,u_name=?,u_age=?,u_phone=?,u_status=? where u_id=?";
        try {
            qur.update(sql, user.getU_account(),user.getU_password(),user.getU_name(),user.getU_age(),user.getU_phone(),user.getU_status(),user.getU_id());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }

    @Override
    public int findAllUserCount() {
        String sql = "select * from user";
        try {
            List<User> query = qur.query(sql, new BeanListHandler<>(User.class));
            return query.size();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<User> findAllUser(int currentPage) {
        String sql = "select * from user limit ?,5";
        List<User> list = null;
        try {
            list = qur.query(sql, new BeanListHandler<>(User.class), (currentPage - 1) * 5);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}
